import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import { DataService } from '../../../../../services/data.service'
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { ApplyQDIINameComponent } from '../../modal/apply-qdiiname/apply-qdiiname.component';
import { SearchService } from './search.service';
@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss'],
  providers: [SearchService]
})
export class SearchComponent implements OnInit {
  // 基金代码modal
  @ViewChild("applyQDIIFundCode", { read: ViewContainerRef })
  applyQDIIFundCode: ViewContainerRef;
  applyQDIINameComponent: ComponentRef<ApplyQDIINameComponent>;
  //获取搜索条件字典值
  public dictionary: any = {
    RISK_BOOLMBERG_CONTROL: [],//bloomberg系统能否控制
    QDII_RECORD_TYPE: [],//风控条目属性
    QD_ACCOUNT_TYPE: [],//适用基金类别
  }
  public getBody: any = {
    pageIndex: 1,
    pageSize: 50,
    nQdIdList: '',
    sValid: '',
    sFileSource: '',
    sFileName: '',
    sItemType: '',
    sFundType: '',
    sFundGroupds: '',
    nQdId: '',
    f1: '',
    f3: '',
    f4: '',
    f5: '',
    f6: '',
    f7: '',
    f8: '',
    f9: '',
    f10: '',
  }
  //基金代码展示转义中文
  public sFundGroupdsZh: any;
  //基金组下拉列表
  public groupList: any = [];
  //基金组下拉框绑定数据
  public groupKey: any = '';
  constructor(
    public resolver: ComponentFactoryResolver,
    public dataService: DataService,
    public searchService: SearchService
  ) { }

  ngOnInit() {
    let that = this;
    //通过服务获取字典值
    this.dictionary = JSON.parse(localStorage.getItem("dictionaryList"));
    this.dataService.QDIIInputModelEmitter = new EventEmitter();
    this.dataService.QDIIInputModelEmitter.subscribe((value: any) => {
      if (value) {
        switch (value) {
          case 'searchFundCode':
            that.getBody.sFundGroupds = that.dataService.QDIIIdList;
            that.sFundGroupdsZh = that.dataService.QDIINameList;
            window['$']('#applyQDIIFundCode').modal('hide');
            break;
        }
      }
    })
    this.fundGroupList();
  }

  searchBody() {
    // console.log("================");
    // console.log(this.getBody);
    if(this.getBody.sFundGroupds != '' &&this.getBody.sFundGroupds != null&&this.getBody.sFundGroupds != undefined){
      if(this.groupKey != ''){
        this.getBody.sFundGroupds = _.cloneDeep(this.getBody.sFundGroupds + ',' + this.groupKey)
      }
    }else{
      if(this.groupKey != ''){
        this.getBody.sFundGroupds = _.cloneDeep(this.groupKey)
      }
    }
    this.dataService.searchBody = _.cloneDeep(this.getBody);
    console.log(this.getBody)
    this.dataService.QDIIReadInfoEmitter.emit('searchBody');
  }
  //基金代码modal
  showApplyQDIIFundCode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.applyQDIINameComponent && this.applyQDIINameComponent.instance) {
      this.applyQDIINameComponent.destroy();
    }
    // 动态创建组件的实例
    const applyQDIINameComponent = this.resolver.resolveComponentFactory(ApplyQDIINameComponent);
    this.applyQDIINameComponent = this.applyQDIIFundCode.createComponent(applyQDIINameComponent);
    this.applyQDIINameComponent.instance.modeType = 'searchFundCode';
    if (this.getBody.sFundGroupds != '' && this.getBody.sFundGroupds != null && this.getBody.sFundGroupds != undefined) {
      this.applyQDIINameComponent.instance.checkedIdList = this.getBody.sFundGroupds.split(',');
      // console.log(this.getBody.sFundGroupds.split(','))
    }

    window['$']('#applyQDIIFundCode').modal('show');
  }
  //风控条目编号正整数控制
  integerControl() {
    setTimeout(() => {
      this.getBody.nQdId = this.getBody.nQdId.replace(/[^0-9]/g, '');
    });
  }
  //获取基金组列表列表
  fundGroupList() {
    let that = this;
    let getBody = {
      sType: 1  //1代表QDII，0代表非QDII
    }
    this.searchService.fundGroupList(getBody).subscribe(data => {
      if (data) {
        this.groupList = data;
      }
    })
  }
}
